package com.sc.nft.admin.request.ecologypool;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sc.nft.enums.ecologypool.EcologyPoolRedPackMechanismEnum;
import com.sc.nft.enums.ecologypool.EcologyPoolRedPackTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.util.LocalDateTimeUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.validator.constraints.Range;

import javax.validation.constraints.Digits;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Data
public class EcologyPoolRedPackCreateRequest implements Serializable {

    @ApiModelProperty("daoId")
    @NotNull(message = "daoId不能为空")
    private Long daoId;

    @ApiModelProperty("名称")
    @NotBlank(message = "名称不能为空")
    private String name;

    @ApiModelProperty("奖励机制")
    @NotNull(message = "奖励机制不能为空")
    private EcologyPoolRedPackMechanismEnum mechanism;

    @ApiModelProperty("奖励类型")
    @NotNull(message = "奖励类型不能为空")
    private EcologyPoolRedPackTypeEnum type;

    @ApiModelProperty("奖励数量")
    @NotNull(message = "奖励数量不能为空")
    @Range(min = 100, max = 1000000, message = "总数量范围应该该100-1000000之间")
    @Digits(integer = 10, fraction = 0, message = "奖励数量必须是整数")
    private BigDecimal rewardNumber;

    @ApiModelProperty("红包数量")
    @NotNull(message = "红包数量不能为空")
    @Range(min = 1, max = 10000, message = "总数量范围应该该1-10000之间")
    @Digits(integer = 10, fraction = 0, message = "红包数量必须是整数")
    private Long packNumber;

    @ApiModelProperty("开始时间")
    @NotNull(message = "开始时间不能为空")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startTime;

    @ApiModelProperty("结束时间")
    @NotNull(message = "结束时间不能为空")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private Date endTime;

    @ApiModelProperty("活动规则")
    @NotBlank(message = "活动规则不能为空")
    private String activeRule;

    @ApiModelProperty("参与条件")
    @NotNull(message = "参与条件不能为空")
    private Boolean participate;

    @ApiModelProperty("个人参与次数")
    @NotNull(message = "个人参与次数不能为空")
    @Range(min = 1, message = "个人参与次数最小为1")
    @Digits(integer = 10, fraction = 0, message = "个人参与次数必须是整数")
    private Integer participateNumber;

    @ApiModelProperty("参与nftId")
    private List<Long> participateNft;

    public void checkStartTime() {
        LocalDateTime startTimeLocal = LocalDateTimeUtil.dateToLocalDateTime(startTime);
        LocalDateTime endTimeLocal = LocalDateTimeUtil.dateToLocalDateTime(endTime);
        if (startTimeLocal.isBefore(LocalDateTime.now())) {
            throw new GlobalRunTimeException("活动开启时间必须大于当前时间");
        }

        if (endTimeLocal.isBefore((startTimeLocal))) {
            throw new GlobalRunTimeException("活动关闭时间必须大于活动开启时间");
        }
    }

    public void checkNameLength() {
        if (name.length() > 10) {
            throw new GlobalRunTimeException("名称最多十个字");
        }
    }


    public void checkParticipateNft() {
        if (participate && CollectionUtils.isEmpty(participateNft)) {
            throw new GlobalRunTimeException("有参与条件的情况下,参与藏品不能为空");
        }
    }
}
